package com.yoomiito.app.ui.my.songdudouo

import android.os.Bundle
import android.view.View
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.my.GiveMoreIncomeDetail
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.Const
import com.yoomiito.app.widget.dialog.TipDialog

class GiveMoreIncomeFragment : MyOrderBaseFragment<GiveMoreIncomeFragmentPresenter>() {
    private var mCurrentType = 0
    private val mAdapter = GiveMoreIncomeAdapter(null)
    private var mSelectOrderId = "" //选中的item的orderId
    private var mSelectPosition = 1//选中的item的position
    private val mTipDialog: TipDialog by lazy {
        ////申请结算弹窗
        val it = TipDialog(context)
        it.setCancelText("取消")
        it.setSureText("申请结算")
        it.setClickListener {
            showLoading()
            p?.submitSettlement(mSelectOrderId)
        }
        it.setTitleVisible(false)
        it
    }

    override fun getNewData() {
        p.getData(currentPage, mCurrentType)
    }

    override fun getEmptyContent(): String = "暂无收益"

    override fun newP(): GiveMoreIncomeFragmentPresenter = GiveMoreIncomeFragmentPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mCurrentType = arguments?.getInt(Const.Common.Key)!!
        mAdapter.type = mCurrentType
        mRecyclerView.adapter = mAdapter
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val data = mAdapter.data[position]
            mSelectOrderId = data.order_id.toString()
            when (view.id) {
                R.id.submitTv -> {
                    mSelectPosition = position
                    val money = if (data.team_status==1) data.user_income else AllUtils.priceDiv(data.user_income, "2")
                    mTipDialog.setContent("申请把等待结算的${money}马上结算为可抵扣余额，一经结算不可撤回")
                    mTipDialog.show()
                }
                R.id.tipIv -> {
                    showLoading()
                    p?.getTip(view, mSelectOrderId)
                }
            }
        }
    }

    fun setData(data: BaseList<GiveMoreIncomeDetail>?) {
        dismissLoading()
        (context as GiveMoreIncomeActivity).moneyTv.text = data?.send_more_income
        if (data?.current_page == 1) {
            if (data?.data.size > 0) {
                mAdapter.setNewData(data?.data)
                mStatusManager.showContent()
            } else {
                mStatusManager.showEmpty()
            }
            mSmartRefreshLayout.finishRefresh(true)
        } else {
            mAdapter.addData(data?.data!!)
            mSmartRefreshLayout.finishLoadMore(true)
        }
        if (data?.current_page == data?.last_page) {
            mSmartRefreshLayout.finishLoadMoreWithNoMoreData()
        }
        currentPage = data?.current_page!!
    }

    fun setTip(view: View, tips: List<String>?) {
        dismissLoading()
//        tip?.showToast()
        if (tips?.size!! > 0) {
            var tip = ""
            tips.forEachIndexed { index, s ->
                tip = if (index == 0) {
                    s
                } else {
                    tip + "\n" + s
                }
            }
            showTip(view, tip, 2)
        }

    }

    fun settleResult(result: Boolean) {
        dismissLoading()
        mTipDialog.dismiss()
        if (result) {
            //申请成功要刷新数据
            val itemData = mAdapter.data[mSelectPosition]
            itemData.status = 1
            mAdapter.notifyItemChanged(mSelectPosition)
//            val page = when{
//                mSelectPosition<20 -> 1
//                mSelectPosition%20==0 -> mSelectPosition/20
//                else ->  mSelectPosition/20+1
//            }
//            p.getData(page,mCurrentType)

        }

    }

//    fun refreshPartData(data:BaseList<GiveMoreIncomeDetail>?){
//
//    }

    fun finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true)
        mSmartRefreshLayout.finishLoadMore(true)
    }
}